<template>
  <div class="finance-manager">
    <!-- 页面标题区域 -->
    <div class="page-header">
      <h2>融资申请管理</h2>
      <div class="stats-tag">共 {{ total }} 条申请</div>
    </div>

    <!-- 搜索和操作区域 -->
    <div class="operation-area">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="用户名">
          <el-input v-model="searchForm.username" placeholder="请输入用户名" clearable></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="全部" value=""></el-option>
            <el-option label="待审核" value="PENDING"></el-option>
            <el-option label="已通过" value="APPROVED"></el-option>
            <el-option label="已拒绝" value="REJECTED"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="medium" type="success" @click="handleSearch">查询</el-button>
          <el-button size="medium" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="operation-buttons">
        <el-button size="medium" icon="el-icon-refresh" type="success" plain @click="getList">刷新</el-button>
      </div>
    </div>

    <!-- 融资申请列表 -->
    <div class="table-card">
      <el-table :data="filteredList" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="username" label="用户名" width="120"></el-table-column>
        <el-table-column prop="amount" label="金额" width="120">
          <template slot-scope="scope">
            <span class="amount-text">¥{{ scope.row.amount }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="reason" label="说明"></el-table-column>
        <el-table-column prop="status" label="状态" width="120">
          <template slot-scope="scope">
            <el-tag :type="statusTagType(scope.row.status)" effect="light">
              {{ statusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="applyTime" label="申请时间" width="180">
          <template slot-scope="scope">
            {{ formatTime(scope.row.applyTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="approveTime" label="审核时间" width="180">
          <template slot-scope="scope">
            {{ scope.row.approveTime ? formatTime(scope.row.approveTime) : '-' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right">
          <template slot-scope="scope">
            <el-button type="success" size="small" :disabled="scope.row.status !== 'PENDING'" @click="approve(scope.row)">通过</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.currentPage" :limit.sync="queryParams.size" @pagination="getList" />
    </div>
    
    <!-- 装饰元素 -->
    <div class="decoration-element"></div>
    <div class="decoration-element-2"></div>
  </div>
</template>

<script>
import request from '@/utils/request'
import Pagination from '@/components/Pagination/index.vue'

export default {
  name: 'FinanceManager',
  components: { Pagination },
  data() {
    return {
      searchForm: {
        username: '',
        status: ''
      },
      queryParams: {
        currentPage: 1,
        size: 10
      },
      list: [],
      total: 0
    }
  },
  computed: {
    filteredList() {
      let arr = this.list
      if (this.searchForm.username) {
        arr = arr.filter(item => item.username && item.username.includes(this.searchForm.username))
      }
      if (this.searchForm.status) {
        arr = arr.filter(item => item.status === this.searchForm.status)
      }
      // 分页
      const start = (this.queryParams.currentPage - 1) * this.queryParams.size
      return arr.slice(start, start + this.queryParams.size)
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      try {
        const res = await request.get('/finance/all')
        if (Array.isArray(res)) {
          this.list = res.sort((a, b) => b.id - a.id)
          this.total = this.list.length
        }
      } catch (e) {
        this.$message.error('获取数据失败')
      }
    },
    handleSearch() {
      this.queryParams.currentPage = 1
    },
    resetSearch() {
      this.searchForm = { username: '', status: '' }
      this.handleSearch()
    },
    async approve(row) {
      try {
        await this.$confirm('确定要通过此融资申请吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        
        await request.post(`/finance/approve/${row.id}`)
        this.$message.success('审核通过')
        this.getList()
      } catch (e) {
        if (e !== 'cancel') {
          this.$message.error('操作失败')
        }
      }
    },
    statusText(status) {
      switch (status) {
        case 'PENDING': return '待审核'
        case 'APPROVED': return '已通过'
        case 'REJECTED': return '已拒绝'
        default: return status
      }
    },
    statusTagType(status) {
      switch (status) {
        case 'PENDING': return 'warning'
        case 'APPROVED': return 'success'
        case 'REJECTED': return 'danger'
        default: return 'info'
      }
    },
    formatTime(time) {
      if (!time) return '-'
      const d = new Date(time)
      return d.toLocaleString()
    }
  }
}
</script>

<style scoped>
.finance-manager {
  padding: 24px;
  background-color: #f5f7fa;
  min-height: 100vh;
  position: relative;
  color: #333;
}

.page-header {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
  padding: 20px;
  background-color: #ffffff;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.page-header h2 {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0 15px 0 0;
  position: relative;
}

.page-header h2::after {
  content: "";
  position: absolute;
  bottom: -8px;
  left: 0;
  width: 40px;
  height: 3px;
  background: linear-gradient(90deg, #4caf50, #81c784);
  border-radius: 3px;
}

.stats-tag {
  background-color: #f0f0f0;
  color: #666;
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
}

.operation-area {
  background-color: #ffffff;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  margin-bottom: 24px;
  padding: 20px;
}

.table-card {
  background-color: #ffffff;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  padding: 20px;
  margin-bottom: 24px;
}

.operation-buttons {
  margin-top: 20px;
  display: flex;
  gap: 12px;
}

.amount-text {
  color: #4caf50;
  font-weight: 600;
}

/* 表格样式 */
:deep(.el-table) {
  background-color: #ffffff;
  color: #333;
  border: none;
}

:deep(.el-table th) {
  background-color: #f5f7fa;
  color: #333;
  font-weight: 500;
  border-bottom: 1px solid #ebeef5;
}

:deep(.el-table td) {
  border-bottom: 1px solid #ebeef5;
  background-color: #ffffff;
  padding: 12px 0;
}

:deep(.el-table--border td), 
:deep(.el-table--border th) {
  border-right: 1px solid #ebeef5;
}

:deep(.el-table--border) {
  border: 1px solid #ebeef5;
  border-radius: 6px;
  overflow: hidden;
}

:deep(.el-table--striped .el-table__body tr.el-table__row--striped td) {
  background-color: #fafafa;
}

:deep(.el-table__body tr.hover-row > td) {
  background-color: #f5f7fa !important;
}

/* 表单元素样式 */
:deep(.el-form-item__label) {
  color: #606266;
}

:deep(.el-input__inner) {
  background-color: #ffffff;
  border-color: #dcdfe6;
  color: #333;
}

:deep(.el-input__inner:focus) {
  border-color: #4caf50;
}

:deep(.el-select .el-input.is-focus .el-input__inner) {
  border-color: #4caf50;
}

/* 标签样式 */
:deep(.el-tag--success) {
  background-color: rgba(76, 175, 80, 0.1);
  border-color: rgba(76, 175, 80, 0.2);
  color: #4caf50;
}

:deep(.el-tag--warning) {
  background-color: rgba(230, 162, 60, 0.1);
  border-color: rgba(230, 162, 60, 0.2);
  color: #e6a23c;
}

:deep(.el-tag--danger) {
  background-color: rgba(245, 108, 108, 0.1);
  border-color: rgba(245, 108, 108, 0.2);
  color: #f56c6c;
}

/* 按钮样式 */
:deep(.el-button) {
  border-radius: 6px;
  transition: all 0.3s ease;
}

:deep(.el-button--success) {
  background-color: #4caf50;
  border-color: #4caf50;
  color: #ffffff;
}

:deep(.el-button--success:hover) {
  background-color: #2e7d32;
  border-color: #2e7d32;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3);
}

:deep(.el-button--success:disabled) {
  background-color: #f5f7fa;
  color: #c0c4cc;
  border: 1px solid #dcdfe6;
}

/* 分页样式 */
:deep(.el-pagination) {
  margin-top: 20px;
  text-align: center;
  color: #606266;
}

:deep(.el-pagination button) {
  background-color: #ffffff;
  color: #606266;
}

:deep(.el-pagination .el-pager li) {
  background-color: #ffffff;
  color: #606266;
  border: 1px solid #dcdfe6;
}

:deep(.el-pagination .el-pager li.active) {
  background-color: #4caf50;
  color: #ffffff;
  border-color: #4caf50;
}

/* 装饰元素 */
.decoration-element {
  position: absolute;
  right: 5%;
  bottom: 10%;
  width: 300px;
  height: 300px;
  background: linear-gradient(135deg, rgba(76, 175, 80, 0.05) 0%, rgba(129, 199, 132, 0.01) 100%);
  border-radius: 50%;
  z-index: -1;
  filter: blur(60px);
  pointer-events: none;
}

.decoration-element-2 {
  position: absolute;
  left: 10%;
  top: 20%;
  width: 200px;
  height: 200px;
  background: linear-gradient(135deg, rgba(76, 175, 80, 0.05) 0%, rgba(129, 199, 132, 0.01) 100%);
  border-radius: 50%;
  z-index: -1;
  filter: blur(40px);
  pointer-events: none;
}
</style> 